package com.xt.stock.project.controlle.user;

import com.xt.stock.project.common.annotation.ApiStatistic;
import com.xt.stock.project.common.basic.GlobalResult;
import com.xt.stock.project.common.constant.XtCodeConstant;
import com.xt.stock.project.common.exception.XtServiceException;
import com.xt.stock.project.service.user.LoginService;
import com.xt.stock.project.vo.user.LoginUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录注销
 */
@Api("登录注销")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiStatistic
    @ApiOperation(value = "/login", notes="用户登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public GlobalResult login(LoginUserVo vo) {
        //用户名非空校验
        if(StringUtils.isEmpty(vo.getUserName())) {
            return GlobalResult.fail(XtCodeConstant.ERROR_NULL_DATA,"用户名不能为空!");
        }
        //密码非空校验
        if(StringUtils.isEmpty(vo.getPassword())) {
            return GlobalResult.fail(XtCodeConstant.ERROR_NULL_DATA,"用户密码不能为空!");
        }

        try {
            //用户登录流程
            loginService.login(vo.getUserName(),vo.getPassword());
            return GlobalResult.success(XtCodeConstant.TRUE_RETURN,"用户登录成功!");
        } catch (XtServiceException e) {
            return GlobalResult.fail(e.getErrorCode(),e.getMessage());
        }
    }

    @ApiOperation(value = "/signOut", notes="注销登录")
    @RequestMapping(value = "/signOut", method = RequestMethod.POST)
    public GlobalResult signOut() {
        try {
            //注销登录
            loginService.signOut();
            return GlobalResult.success(XtCodeConstant.TRUE_RETURN,"注销成功!");
        } catch (XtServiceException e) {
            return GlobalResult.fail(e.getErrorCode(),e.getMessage());
        }
    }

}
